/*
 * @Author: Marte
 * @Date:   2016-10-27 14:25:34
 * @Last Modified by:   Marte
 * @Last Modified time: 2016-11-14 11:12:27
 */

'use strict';

var currApp = angular.module('addpicremark.app', ['ui.bootstrap', 'ngFileUpload', 'qf.common']);

currApp.controller('pagectrl', ['$scope', '$http', '$window', '$timeout', 'Upload', 'dialogService', 'defendAgain',
    function($scope, $http, $window, $timeout, Upload, dialogService, defendAgain) {
        $scope.globalParam = globalParam;

        var vm = this;
        vm.input = {
            userId: $scope.globalParam.uid,
            picGroup: [{
                id: null,
                groupName: null,
                pic: [{
                    id: null,
                    fileName: null
                }]
            }]
        };
        /*获取证件类型*/
        $http.get('api/types')
            .success(function(data, status, headers, config) {
                vm.types = data.error ? null : data;
            })
            .error(function(data, status, headers, config) {
                vm.types = null;
            });

        /*获取评语*/
        $http.get('api/remarks')
            .success(function(data, status, headers, config) {
                vm.remarks = data.error ? null : data;
            })
            .error(function(data, status, headers, config) {
                vm.remarks = null;
            });

        /*添加分组*/
        vm.addGroup = function() {
            vm.input.picGroup.push({
                groupId: null,
                groupName: null,
                pic: [{
                    id: null,
                    fileName: null
                }]
            });
        };

        //保存表单
        vm.submit = function() {
            defendAgain.notifyActionWorking();
            $http.post('api/pictyperemarkgroups', vm.input)
                .success(function(data, status, headers, config) {
                    defendAgain.notifyActionWorkOver();
                    if (data.error) {
                        dialogService.showmessage({ msg: data.error });
                    } else {
                        $window.location.replace('index.html?' + $scope.globalParam.paramString());
                    }
                })
                .error(function(data, status, headers, config) {
                    defendAgain.notifyActionWorkOver();
                    dialogService.showmessage({ msg: '提交任务失败' });
                });
        };

        //文件上传
        $scope.uploadFiles = function(files, picGroup) {
            var failFiles = '';

            if (files && files.length) {
                Upload.upload({
                        url: 'api/pics',
                        data: { file: files }
                    })
                    .success(function (data, status, headers, config) {
                        //console.log(data);
                        if (data.error) {
                            dialogService.showmessage({ msg: data.error });
                        } else if (data.length > 0) {
                            
                            for (var i = 0; i < data.length; i++) {
                                picGroup.pic.splice(picGroup.pic.length - 1, 0, {
                                    id: data[i].id,
                                    fileName: data[i].fileName
                                });
                            }
                        }
                        
                    })
                    .error(function(data, status, headers, config) {
                        dialogService.showmessage({ msg: '上传失败' });
                    });
                //改为一次上传一批文件
                /*for (var i = 0; i < files.length; i++) {
                    var fileName = files[i].name;
                    Upload.upload({
                            url: 'api/pics',
                            data: { file: files[i] }
                        })
                        .success(function(data, status, headers, config) {
                            if (data[0].ID) {
                                picGroup.pic[picGroup.pic.length - 1] = {
                                    id: data[0].ID,
                                    fileName: data[0].FILENAME
                                };
                                picGroup.pic.push({});
                            } else {
                                failFiles += ((failFiles === '') ? '' : ',') + fileName;
                            }
                        })
                        .error(function(data, status, headers, config) {
                            failFiles += ((failFiles === '') ? '' : ',') + fileName;
                            console.log(fileName + '上传失败');
                        });
                }*/

            }

        };

        //删除文件
        $scope.deleteFile = function(picGroup, index) {
            $http({ method: 'DELETE', url: 'api/pics/' + picGroup.pic[index].id })
                .success(function(data, status, headers, config) {
                    if (data.success === 'OK') {
                        picGroup.pic.splice(index, 1);
                    } else if (data.error) {
                        dialogService.showmessage({ msg: data.error });
                    }
                })
                .error(function(data, status, headers, config) {
                    dialogService.showmessage({ msg: '删除相片失败' });
                });
        };

        //测试方法
        /*        vm.test = function() {
                    console.log(vm.input.picGroup);
                };*/

    }
]);
